package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.domain.VIP_Management;
import com.it.service.VIP_ManagementService;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("vipM")
public class VIPManagementController {

    @Autowired
    private VIP_ManagementService vipManagementService;

    //新增
    @RequestMapping("/save.do")
    public ModelAndView save(VIP_Management vipManagement, HttpServletRequest request, HttpServletResponse response){

        vipManagement.setName(request.getParameter("name"));
        vipManagement.setAccountnumber(request.getParameter("accountnumber"));
        vipManagement.setContact(request.getParameter("contact"));
        vipManagement.setIntegral(0);

        System.out.println("后台得到数据："+vipManagement);

        vipManagementService.saveVIPer(vipManagement);

        String s = ("注册成功！");
        ModelAndView mv = new ModelAndView();
        mv.addObject("return_as",s);
        mv.setViewName("returnBack");
        return mv;

    }

    /**
     *查询所有
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findAll.do")
    /*@ResponseBody*/
    public ModelAndView FindAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,@RequestParam(name = "size",required = true,defaultValue = "4")int size, Model model, VIP_Management vipManagement, HttpServletRequest request, HttpServletResponse response){

        ModelAndView mv = new ModelAndView();

        List<VIP_Management> list = vipManagementService.FindAllVIP(page,size);

        //PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("viplist",pageInfo);
        mv.setViewName("vip_findAll");

        return mv;

    }

    /**
     * 根据id查询
     * @param page
     * @param size
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/FindById.do")
    public ModelAndView FindById(@RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                         @RequestParam(name = "size",required = true,defaultValue = "4")int size,
                                         Model model, VIP_Management vipManagement, HttpServletRequest request,
                                         HttpServletResponse response){

        //获取前端传过来的id
        ModelAndView mv = new ModelAndView();
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);


        List<VIP_Management> IdList = vipManagementService.FindById(id,page,size);

        //PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(IdList);
        mv.addObject("vipFindByIdlist",pageInfo);
        mv.setViewName("vip_findById");

        return mv;
    }

    /**
     * 根据id删除会员信息
     * @param page
     * @param size
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delVip.do")
    public ModelAndView delVip(@RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                 @RequestParam(name = "size",required = true,defaultValue = "4")int size,
                                 Model model, VIP_Management vipManagement, HttpServletRequest request,
                                 HttpServletResponse response){
        //获取前端传过来的id
        ModelAndView mv = new ModelAndView();
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);

        boolean b = vipManagementService.delVip(id);
        //判断   true为删除成功
        if (b){
            String delR = "删除成功！";
             mv.addObject("return_as",delR);
             mv.setViewName("returnBack");
            return mv;
        }else {
            String delR = "删除失败！";
            mv.addObject("return_as",delR);
            mv.setViewName("returnBack");
            return mv;
        }

    }

    /**
     * 根据id修改信息
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/changeVip")
    public  ModelAndView ChangeVip(Model model, VIP_Management vipManagement, HttpServletRequest request,
                                HttpServletResponse response){
        ModelAndView mv = new ModelAndView();
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);
        List<VIP_Management> list = vipManagementService.FindById(id, 1, 4);

        if (null == list || list.size() ==0 ){
            //为空
            String delR = "修改失败！可能无此id！";
            mv.addObject("return_as",delR);
            mv.setViewName("returnBack");
            return mv;
        }else {
            //插入新信息
            vipManagement.setId(id);
            vipManagement.setName(request.getParameter("name"));
            vipManagement.setAccountnumber(request.getParameter("accountnumber"));
            vipManagement.setContact(request.getParameter("contact"));

            //获取原本积分信息，放入数据不变
            VIP_Management vip_management = list.get(0);
            Integer integral = vip_management.getIntegral();
            vipManagement.setIntegral(integral);

            vipManagementService.changeVip(vipManagement);

            String delR = "修改成功！";
            mv.addObject("return_as",delR);
            mv.setViewName("returnBack");

            return mv;

        }
    }

    /**
     * 根据姓名模糊查询
     * @param page
     * @param size
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findbyname.do")
    public ModelAndView FindByName(@RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                    @RequestParam(name = "size",required = true,defaultValue = "5")int size,
                                    Model model, VIP_Management vipManagement, HttpServletRequest request,
                                   HttpServletResponse response){
        String s = request.getParameter("name");
        ModelAndView mv = new ModelAndView();
        List<VIP_Management> list = vipManagementService.FindByName(s,page,size);

        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("FindByName",pageInfo);
        mv.setViewName("vip_FindByName");

        return mv;
    }

    /**
     * 会员积分加减
     * @param model
     * @param vipManagement
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/vipAddOrSub.do")
    public ModelAndView FindByName( Model model, VIP_Management vipManagement, HttpServletRequest request,
                                   HttpServletResponse response){
        String s = "修改成功！";
        ModelAndView mv = new ModelAndView();
        System.out.println("开始");
        //将数据取出
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        try {
            int num = Integer.parseInt(request.getParameter("num"));
            System.out.println(num);
            //根据id取出相应数据
            List<VIP_Management> list = vipManagementService.FindById(id, 1, 4);
            System.out.println(list);

            //将数据拿出，然后进行加减
            VIP_Management vip_management = list.get(0);
            System.out.println(vip_management);
            Integer integral = vip_management.getIntegral()+num;
            System.out.println(integral);
            vip_management.setIntegral(integral);

            //将数据传回,在数据库中进行更新
            vipManagementService.changeVip(vip_management);
            System.out.println("更新完");
        }catch (Exception e){
            System.out.println("错误");
            s = "修改失败！";
        }






        mv.addObject("return_as",s);
        mv.setViewName("returnBack");

        return mv;

    }

}
